String clazzTpl({package}) => """
import 'dart:io';
import 'app_debug_model.dart';
import 'package:xanno/xanno.dart';
import 'package:flutter/material.dart';
import 'package:$package/route/main.route.dart';
import 'package:$package/widget/theme_builder.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:$package/widget/base_store_container.dart';

@GRoute(url: '/appErrorLog', title: 'AppErrorLog')
class AppErrorLogPage extends StatefulWidget {
  final String title;

  const AppErrorLogPage({Key? key, required this.title}) : super(key: key);

  @override
  State<AppErrorLogPage> createState() => _AppErrorLogPageState();
}

class _AppErrorLogPageState extends State<AppErrorLogPage> {
  List<FileSystemEntity> _files = [];
  List<String> _contentList = [];
  String _title = '';
  Image? _image;

  // 对图片格式的显示
  final List<String> _suffix = [
    "jpg",
    "jpeg",
    "gif",
    "png",
    "webp",
    "bmp",
    "wbmp",
  ];

  void _deleteItem(BuildContext context, FileSystemEntity item) {
    final fileOrDir =
        FileSystemEntity.isFileSync(item.path) ? 'file' : 'directory';
    final fileName = item.path.split(Platform.pathSeparator).last;
    ScaffoldMessenger.of(context)
      ..hideCurrentSnackBar()
      ..showSnackBar(
          SnackBar(content: Text('delete \$fileOrDir [\$fileName] success')));
    File(item.path).deleteSync(recursive: true);
    setState(() {
      _files.remove(item);
    });
  }

  Widget _fileContent(Color titleColor, Color bodyColor) {
    return ListView.builder(
      physics:
          const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
      padding: const EdgeInsets.all(16),
      itemCount: _contentList.length,
      itemBuilder: (context, index) =>
          Text(_contentList[index], style: TextStyle(color: titleColor)),
    );
  }

  Widget _dirList(Color titleColor, Color bodyColor) {
    return SlidableAutoCloseBehavior(
      child: ListView.separated(
        physics: const BouncingScrollPhysics(
            parent: AlwaysScrollableScrollPhysics()),
        itemCount: _files.length,
        separatorBuilder: (BuildContext context, int index) {
          return const Divider(height: 1);
        },
        itemBuilder: (BuildContext context, int index) {
          FileSystemEntity item = _files[index];
          bool isFile = FileSystemEntity.isFileSync(item.path);
          String sizeStr = '';
          if (isFile) {
            double size = File(item.path).statSync().size / 1024;
            if (size > 1024) {
              size /= 1024;
              if (size > 1024) {
                sizeStr = "\${size.toStringAsFixed(2)}GB";
              } else {
                sizeStr = "\${size.toStringAsFixed(2)}MB";
              }
            } else {
              sizeStr = "\${size.toStringAsFixed(2)}KB";
            }
          }
          return GestureDetector(
            onTap: () async {
              final name = item.path.split(Platform.pathSeparator).last;
              if (FileSystemEntity.isFileSync(item.path)) {
                Navigator.pushNamed(context, RouteConstant.AppErrorLogPage,
                    arguments: ErrorLogFileModel(
                        path: item.path,
                        title: name.split('.').first,
                        isFile: true));
              } else {
                Navigator.pushNamed(context, RouteConstant.AppErrorLogPage,
                    arguments: ErrorLogFileModel(
                        path: item.path, title: name, isFile: false));
              }
            },
            child: Slidable(
              key: Key(item.path),
              groupTag: "groupTag",
              endActionPane: ActionPane(
                motion: const DrawerMotion(),
                extentRatio: 0.25,
                children: [
                  CustomSlidableAction(
                    onPressed: (context) => _deleteItem(context, item),
                    backgroundColor: Colors.red,
                    child: const Text(
                      '删除',
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                ],
              ),
              child: Container(
                color: titleColor.withOpacity(0.8),
                child: ListTile(
                  title: Text(
                    item.path.split(Platform.pathSeparator).last,
                    style: TextStyle(
                        color: bodyColor, fontWeight: FontWeight.bold),
                  ),
                  subtitle: isFile
                      ? Text(
                          sizeStr,
                          style: const TextStyle(color: Colors.white),
                        )
                      : null,
                  trailing: Icon(Icons.keyboard_arrow_right,
                      size: 30, color: bodyColor),
                ),
              ),
            ),
          );
        },
      ),
    );
  }

  @override
  void didChangeDependencies() {
    // AppDebugModel
    final model = ModalRoute.of(context)!.settings.arguments;
    if (model is ErrorLogFileModel) {
      _title = model.title;
      if (model.isFile) {
        if (_suffix.indexWhere((e) =>
                e.toUpperCase() == model.path.split(".").last.toUpperCase()) >=
            0) {
          _image = Image.file(File(model.path));
        } else {
          File(model.path).readAsLines().then((value) {
            setState(() {
              _contentList = value;
            });
          });
        }
      } else {
        //create time sort
        Directory(model.path).list().toList().then((value) {
          value.sort((a, b) =>
              (b.statSync().modified).compareTo(a.statSync().modified));
          setState(() {
            _files = value;
          });
        });
      }
    } else if (model is AppDebugModel) {
      setState(() {
        model.childList?.forEach((item) {
          _contentList.add("\${item.key}=\${item.value}");
        });
        _title = model.title;
      });
    }
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    return ThemeBuilder(builder: (context, theme) {
      return SelectionArea(
        child: Scaffold(
          backgroundColor: theme.body,
          appBar: AppBar(
            systemOverlayStyle: theme.systemOverlayStyleReverse,
            backgroundColor: theme.body,
            iconTheme: IconThemeData(color: theme.title, opacity: 1),
            title: Text(_title, style: TextStyle(color: theme.title)),
          ),
          body: _files.isEmpty
              ? _image ?? _fileContent(theme.title, theme.body)
              : _dirList(theme.title, theme.body),
        ),
      );
    });
  }
}
""";
